package com.raindrop.rd;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * RD语言引擎工厂
 *
 * @author BillDowney
 * @date 2021/3/22 14:47
 */
public class RDScriptEngineFactory implements ScriptEngineFactory {
    @Override
    public String getEngineName() {
        return "RDScript";
    }

    @Override
    public String getEngineVersion() {
        return "0.0.1";
    }

    @Override
    public List<String> getExtensions() {
        return Arrays.asList("rd", "rdm");
    }

    @Override
    public List<String> getMimeTypes() {
        return Arrays.asList("application/rd-script", "text/rd-script");
    }

    @Override
    public List<String> getNames() {
        return Arrays.asList("RDScript", "rd-script");
    }

    @Override
    public String getLanguageName() {
        return "RDScript";
    }

    @Override
    public String getLanguageVersion() {
        return Objects.toString(this.getClass().getPackage().getImplementationVersion(), "unknown");
    }

    @Override
    public Object getParameter(String key) {
        if (ScriptEngine.ENGINE.equals(key)) {
            return getEngineName();
        } else if (ScriptEngine.ENGINE_VERSION.equals(key)) {
            return getEngineVersion();
        } else if (ScriptEngine.LANGUAGE_VERSION.equals(key)) {
            return getLanguageVersion();
        } else if (ScriptEngine.LANGUAGE.equals(key)) {
            return getLanguageName();
        }
        throw new IllegalArgumentException("Invalid key:" + key);
    }

    @Override
    public String getMethodCallSyntax(String obj, String m, String... args) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String getOutputStatement(String toDisplay) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String getProgram(String... statements) {
        throw new UnsupportedOperationException();
    }

    @Override
    public ScriptEngine getScriptEngine() {
        return this.getRDScriptEngine();
    }

    public RDScriptEngine getRDScriptEngine() {
        return new RDScriptEngine(this);
    }
}
